package controller.expertInfo.expertEdit;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import controller.common.BaseController;
import controller.common.StaticConstant;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import response.CommonResult;
import services.expertInfo.ExpertEducationServices;
import util.JsonUtil;
import validator.NotBlankFluentValidator;
import validator.NotNullFluentValidator;
import wt.entity.expertInfo.ExpertEducation;

import java.util.List;
import java.util.Map;

;

/**
 * 功能:
 * 作者: 洪世成
 * 日期: 2017/9/9
 * 版权所有: expert
 */
@Controller
@RequestMapping("expertInfo/expertEdit/expertEducation")
public class ExpertSelfEducationController extends BaseController {

    @Autowired
    ExpertEducationServices expertEducationServices;


    @RequiresPermissions("expertInfo:expertEdit:default")
    @RequestMapping("index")
    public String index(Map map) throws Exception {

        List<ExpertEducation> expertEducationList = expertEducationServices.expertSelectByExpertId(getLoginAccount().getExpertBase().getId());
        map.put("data", JsonUtil.obj2Json(expertEducationList));

        return "expertInfo/expertEdit/expertEducation/index";
    }


    @RequiresPermissions("expertInfo:expertEdit:default")
    @RequestMapping("editDlg")
    public String editDlg(String id, Map map) throws Exception {
        if (StringUtils.isNotEmpty(id)) {
            ExpertEducation expertEducation= expertEducationServices.selectByPrimaryKey(id);

            if (expertEducation == null) {
                map.put(StaticConstant.ERROR_MESSAGE, StaticConstant.NOT_FOUND_DATA);
                return StaticConstant.ERROR_DLG;
            }

            map.put(StaticConstant.EDIT_DATA, expertEducation);
            map.put(StaticConstant.EDIT_URL, "expertInfo/expertEdit/expertEducation/update");
            map.put(StaticConstant.EDIT_OPERATE_TYPE, StaticConstant.EDIT_UPDATE);
        } else {
            map.put(StaticConstant.EDIT_URL, "expertInfo/expertEdit/expertEducation/insert");
            map.put(StaticConstant.EDIT_OPERATE_TYPE, StaticConstant.EDIT_INSERT);
        }
        return "expertInfo/expertEdit/expertEducation/editDlg";
    }

    @RequiresPermissions("expertInfo:expertEdit:default")
    @ResponseBody
    @RequestMapping("insert")
    public CommonResult insert(ExpertEducation expertEducation) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        ComplexResult result = FluentValidator.checkAll().failOver()
                .on(expertEducation.getMajor(), new NotBlankFluentValidator("所学专业"))
                .on(expertEducation.getSchool(), new NotBlankFluentValidator("毕业院校"))
                .on(expertEducation.getLearnTime(), new NotNullFluentValidator("年限"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            commonResult.addMessage(result);
            return commonResult.setError();
        }
        expertEducation.setExpertId(getLoginAccount().getExpertBase().getId());
        expertEducationServices.expertInsertSelective(expertEducation);
        return commonResult.setSuccess();
    }


    @RequiresPermissions("expertInfo:expertEdit:default")
    @ResponseBody
    @RequestMapping("update")
    public CommonResult update(ExpertEducation expertEducation) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        ComplexResult result = FluentValidator.checkAll().failOver()
                .on(expertEducation.getMajor(), new NotBlankFluentValidator("所学专业"))
                .on(expertEducation.getSchool(), new NotBlankFluentValidator("毕业院校"))
                .on(expertEducation.getLearnTime(), new NotNullFluentValidator("年限"))

                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            commonResult.addMessage(result);
            return commonResult.setError();
        }
        expertEducation.setExpertId(getLoginAccount().getExpertBase().getId());
        expertEducationServices.expertUpdateByPrimaryKeySelective(expertEducation);
        return commonResult.setSuccess();
    }


    @RequiresPermissions("expertInfo:expertEdit:default")
    @ResponseBody
    @RequestMapping("delete")
    public CommonResult delete(@RequestParam("id") String id) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        if (StringUtils.isBlank(id)) {
            commonResult.addMessage(StaticConstant.DATA_IS_BLANK);
            return commonResult.setError();
        }
        expertEducationServices.expertDeleteByPrimaryKey(id);
        return commonResult.setSuccess();
    }

    @RequiresPermissions("expertInfo:expertEdit:default")
    @ResponseBody
    @RequestMapping("cancelApply")
    public CommonResult cancelApply(@RequestParam("id") String id) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        int result=  expertEducationServices.expertCancelApplyByPrimaryKey(id);
        commonResult.setStatus(result);
        return commonResult;
    }

}
